package com.tangkun.environment.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.tangkun.environment.R;
import com.tangkun.environment.activity.ImageActivity;
import com.tangkun.environment.network.GlobalUrl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import jp.wasabeef.glide.transformations.RoundedCornersTransformation;

/**
 * @author tangkun
 * @ClassName: HuanJingZhaoPianAdapter
 * @Description: 环境照片列表适配器
 * @date 2016-08-20 上午11:57:36
 */
public class HuanJingZhaoPianAdapter extends BaseAdapter {

    private Context context;
    private List<String> dataList = new ArrayList<String>();

    public HuanJingZhaoPianAdapter(Context context, List<String> dataList) {
        this.context = context;
        this.dataList = dataList;
    }

    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public Object getItem(int i) {
        return dataList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        final ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_huanjingzhaopian, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (dataList != null) {
            //图片
            Glide.with(context)
                    .load(GlobalUrl.IMG_URL + dataList.get(position))
                    .bitmapTransform(new RoundedCornersTransformation(context, 5, 5))
                    .centerCrop()
                    .dontAnimate()
                    .placeholder(R.mipmap.ic_place_holder)
                    .into(holder.ivHuanjingzhaopian);

            //查看大图
            convertView.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    Intent i = new Intent(v.getContext(), ImageActivity.class);
                    i.putExtra("urlList", (Serializable) dataList);//图片地址集合
                    i.putExtra("index", position);//索引
                    v.getContext().startActivity(i);
                }
            });
        }
        return convertView;

    }

    /**
     * 自定义接口回调
     */
    private MyClickListener myClickListener;

    public void setMyClickListener(MyClickListener myClickListener) {
        this.myClickListener = myClickListener;
    }

    public interface MyClickListener {
        void onMyClick(int position);
    }

    static class ViewHolder {
        @BindView(R.id.iv_huanjingzhaopian)
        ImageView ivHuanjingzhaopian;
        @BindView(R.id.tv_huanjingzhaopian)
        TextView tvHuanjingzhaopian;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
